package com.music.mysongs.adapter

import android.app.AlertDialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.music.mysongs.data.MusicCreatorData
import com.music.mysongs.data.getApiMusicDelete
import com.music.mysongs.helper.getTimesFormat
import com.music.mysongs.utils.myLog
import com.music.mysongs.utils.myToastWithoutCxt
import com.music.mysongs.web.CommonWebTask

class DeleteRecyclerAdapter(
    cxt: Context,
    private val collectList: ArrayList<MusicCreatorData.MusicList>
) :
    RecyclerView.Adapter<DeleteRecyclerAdapter.MyViewHolder>() {
    private val content = cxt

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        return MyViewHolder(
            LayoutInflater.from(parent.context)
                .inflate(com.music.mysongs.R.layout.item_recycler_del, parent, false)
        )
    }

    override fun getItemCount(): Int {
        return collectList.size
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val itemDel: TextView = view.findViewById(com.music.mysongs.R.id.txtDel_item)
        val itemTitle: TextView = view.findViewById(com.music.mysongs.R.id.txtTitleDel_item)
        val itemTime: TextView = view.findViewById(com.music.mysongs.R.id.txtAddTimeDEl_item)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.itemTitle.text = collectList[position].title
        val format = getTimesFormat(collectList[position].add_time.toInt())
        holder.itemTime.text = format
        holder.itemDel.setOnClickListener {
            AlertDialog.Builder(content).apply {
                setTitle("确定要删除了么?")
                setPositiveButton("手滑了") { _, _ ->
                    myToastWithoutCxt("创作不易，谨慎删除!")
                }
                setNegativeButton("删") { _, _ ->
                    val musicTask = CommonWebTask()
                    musicTask.url = getApiMusicDelete(collectList[position].id.toInt())
                    musicTask.setCallbackHandler { innerData ->
                        val data = Gson().fromJson(innerData, MusicCreatorData.MusicNum::class.java)
                        if (data.code == 20000) {
                            myToastWithoutCxt("删除成功!")
                            collectList.removeAt(position)
                            notifyItemRemoved(position)
                            notifyItemRangeChanged(
                                position,
                                collectList.size - position
                            )
                        } else {
                            myToastWithoutCxt("删除失败!")
                            myLog("DeleteRecyclerAdapter: $data")
                        }
                    }
                    musicTask.execute()
                }
                show()
            }
        }
    }
}